﻿using System;
using System.Collections.Generic;
using System.Text;
using WebServer = SysCommon.Business.CVSS.WS.DCS.WS_DCS_BizCtrlImpl_New;
using Query = SysCommon.Entity.CVSS.WS.DCS.BaseQueryCondition_Ety;
using System.Windows.Forms;
using BindingData = SysCommon.Entity.Business.DCS.View.View_DangerCargo_Ety;
using SaveData = SysCommon.Entity.CVSS.WS.DCS.DangerCargo_Ety;
using CVSS.Client.BDM.Business;
using SysCommon.Interface.ManageUserSession;
using AppCommon.Framework;

namespace CVSS.Client.BDM.UI
{
    partial class DangerCargoManageFormMDT
    {
        private BindingData[] _bindingData = null;
        private SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum _operation;

        #region 初始化DataGridView列表
        private void InitDataGridView()
        {
            try
            {
                BindingData d;
                string[] en_Cols = new string[] { "NAME", "UNITS", "EDITORNAME", "DESCRIPTION", "COUNTERPLAN" };
                string[] cn_Cols = new string[] { "名称", "单位", "编辑人", "货物简介", "应急方案" };
                SysCommon.Tools.ShowEntityForDataGridView.SetDefaultDataGrigStyle2(
                    form.dvp_DataView.dg_DataView);
                SysCommon.Tools.ShowEntityForDataGridView.EntityToView(
                    cn_Cols, en_Cols, new BindingData[0], form.dvp_DataView.dg_DataView);
                form.dvp_DataView.Init(0, 0, 0, null,  cn_Cols,en_Cols);
                form.dvp_DataView.dg_DataView.AutoSizeColumnsMode =
                     DataGridViewAutoSizeColumnsMode.Fill;
                form.btn_Save.Enabled = false;
                form.btn_Modify.Enabled = false;
                form.btn_Delete.Enabled = false;
                SetControlEnable(false);
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.InitDataGridView()出错", ex);
            }
        }
        #endregion

        #region 响应保存事件
        private void Save()
        {
            try
            {
                SaveData data = new SaveData();
                DataGridViewRow row = form.dvp_DataView.dg_DataView.CurrentRow;
                if (row != null)
                {
                    //BindingData bind = form.dvp_DataView.dg_DataView.CurrentRow.DataBoundItem as
                    //    BindingData;
                    //if (bind != null)
                    //{
                    data.ID = (row.Tag as BindingData).ID; 
                    //}
                }
                data.COUNTERPLAN = form.txt_Plan.Text;
                data.DESCRIPTION = form.txt_Description.Text;
                IMUS_ManageUserSessionBizCtrl m_modul =
                    (IMUS_ManageUserSessionBizCtrl)SystemFactory.GetBizController(
                                      typeof(IMUS_ManageUserSessionBizCtrl));
                data.EDITORID = m_modul.GetCurrentUser().ID;
                data.NAME = form.txt_Name.Text;
                data.UNITS = form.txt_Units.Text;
                WebServer ws = new WebServer();
                bool re = ws.DangerCargoOperation(data, _operation);
                if (!re)
                {
                    MessageBox.Show("保存失败。");
                    return;
                }
                form.btn_Modify.Text = "修改";
                form.btn_Add.Text = "添加";
                form.btn_Save.Enabled = false;
                BeginQuery();
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Save()出错", ex);
            }
            finally
            {
                form.btn_Add.Enabled = true;
            }
        }
        #endregion

        #region 响应查询事件
        private void BeginQuery()
        {
            try
            {
                WebServer ws = new WebServer();
                Query query = new Query();
                query.KeyWord = form.txt_KeyWord.Text.Trim();
                byte[] bs = ws.QueryDangerCargoInfoWithCompress(query);
                if (bs != null)
                {
                    BindingData[] ds = SysCommon.Tools.CompressionHelper.DeCompressEntity(bs) as
                        BindingData[];
                    _bindingData = ds;
                    if (_bindingData == null)
                    {
                        _bindingData = new BindingData[0];
                    }
                    ShowEntityHelper.ShowFlowDataInfo<BindingData>(
                        _bindingData, form.dvp_DataView);
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.BeginQuery()出错", ex);
            }
        }
        #endregion

        #region 响应添加事件
        private void Add()
        {
            try
            {
                //判断是否取消添加
                string btnText = form.btn_Add.Text;
                if (btnText.Contains("取消"))  //单击了取消添加按钮
                {                    
                    //把控件设为不可见
                    SetControlEnable(false);
                    form.btn_Save.Enabled = false;
                    //把按钮的Text设为添加
                    form.btn_Add.Text = "添加";
                    //绑定当前选中的项
                    dg_DataView_SelectionChanged(null, null);
                }
                else  //单击了添加按钮
                {
                    _operation = SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum.AddRecord;
                    //把控件设为可见
                    SetControlEnable(true);
                    //设置控件的显示文本为空
                    //名称
                    form.txt_Name.Text = "";
                    //单位
                    form.txt_Units.Text = "";
                    //编辑人
                    form.txt_Editor.Text = "";
                    //货物简介
                    form.txt_Description.Text = "";
                    //应急方案
                    form.txt_Plan.Text = "";
                    //设置按钮的可用性
                    form.btn_Delete.Enabled = false;
                    form.btn_Save.Enabled = true;
                    form.btn_Modify.Enabled = false;
                    form.btn_Add.Text = "取消添加";
                }
            }
            catch (Exception x)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.Add()出错", x);
            }
        }
        #endregion

        #region 响应删除事件
        private void Delete()
        {
            try
            {
                if (MessageBox.Show("确定要删除吗？", "删除", MessageBoxButtons.YesNo,
                     MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }
                //获取当前的选择项
                DataGridViewRow row = form.dvp_DataView.dg_DataView.CurrentRow;
                if (row != null)
                {
                    //BindingData data = row.DataBoundItem as BindingData;

                    //if (data != null)
                    //{
                        SaveData saveData = new SaveData();
                        saveData.ID = (row.Tag as BindingData).ID; 
                        //调用WebService删除信息
                        WebServer ws = new WebServer();
                        bool re=ws.DangerCargoOperation(saveData, SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum.DeleteRecord);
                        if (!re)
                        {
                            MessageBox.Show("删除失败。");
                            return;
                        }
                        BeginQuery();
                    //}
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.Delete()出错", ex);
            }
        }
        #endregion

        #region 响应修改事件
        private void Modify()
        {
            try
            {
                //判断是否取消修改
                string btnText = form.btn_Modify.Text;
                if (btnText.Contains("取消"))  //单击了取消修改按钮
                {
                    //把控件设为不可编辑
                    SetControlEnable(false);
                    form.btn_Add.Enabled = true;
                    //把按钮的Text设为修改
                    form.btn_Modify.Text = "修改";
                    //绑定当前选中的项
                    dg_DataView_SelectionChanged(null, null);
                }
                else
                {
                    _operation = SysCommon.Enums.CVSS.WS.DCS.DBOperatonEnum.ModifyRecord;
                    //把控件设为编辑状态
                    SetControlEnable(true);
                    //把保存按钮设为可用
                    form.btn_Save.Enabled = true;
                    //把添加按钮变为不可用
                    form.btn_Add.Enabled = false;
                    //把删除按钮变为不可用
                    form.btn_Delete.Enabled = false;
                    form.btn_Modify.Text = "取消修改";
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DangerCargoManageFormMDT.Modify()出错", ex);
            }
        }
        #endregion

        #region 数据绑定
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dg_DataView_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow row = form.dvp_DataView.dg_DataView.CurrentRow;
                if (row != null)
                {
                    //检查是否处于编辑状态
                    if (EditorHelper.CheckItem(form.btn_Save,form.btn_Add))
                    {
                        return;
                    }
                    BindingData data = row.Tag as BindingData;
                    if (data != null)
                    {
                        //名称
                        form.txt_Name.Text = data.NAME;
                        //单位
                        form.txt_Units.Text = data.UNITS;
                        //编辑人
                        form.txt_Editor.Text = data.EDITORNAME;
                        //货物简介
                        form.txt_Description.Text = data.DESCRIPTION;
                        //应急方案
                        form.txt_Plan.Text = data.COUNTERPLAN;
                        //设置按钮的可用性
                        form.btn_Delete.Enabled = true;
                        form.btn_Modify.Enabled = true;
                        form.btn_Save.Enabled = false;
                        //设置控件为不可编辑
                        SetControlEnable(false);
                        return;
                    }
                }
                //设置按钮的可用性
                form.btn_Delete.Enabled =true;
                form.btn_Modify.Enabled = true;
                form.btn_Save.Enabled = false;
                form.btn_Add.Enabled = true;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error(
                    "调用DangerEnterpriseManageFormMDT.dg_DataView_SelectionChanged()出错", ex);
            }
        }

        #endregion

        #region 设置控件的可用性
        private void SetControlEnable(bool enable)
        {
            EditorHelper.SetControlEnable(enable,
                new Control[] { form.txt_Name,form.txt_Units,
                        form.txt_Editor,form.txt_Description,form.txt_Plan});
            form.txt_Editor.Enabled = false;
        }
        #endregion
    }
}
